﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

using ScrumTable.Common;
using ScrumTable.Common.Collections;
using ScrumTable.Common.Logging;
using ScrumTable.Common.Monitoring;
using ScrumTable.Common.Net;
using ScrumTable.Common.Security;
using ScrumTable.Common.Text;
using ScrumTable.Common.Threading;
using ScrumTable.DL.Data.Generic.Cache;
using ScrumTable.DL.Data.Schema.ScrumTableV1;
using ScrumTable.DL.Data.TFS.Bulk.Properties;
using ScrumTable.DL.Data.TFS.Services.Reporting;
using ScrumTable.DL.Data.TFS.Services.TeamFoundation;

#endregion

namespace ScrumTable.DL.Data.TFS.Bulk.Services.IterationHandling
{
    /// <summary>
    /// This class is used to load all data below a given iteration.
    /// </summary>
    internal abstract class IterationLoaderBase
    {
        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        internal const string IterationLoadPart = "Iterations";
        protected const char IterationPathSeparator = '\\';

        /// <summary>
        /// Gets the schema configuration data.
        /// </summary>
        protected readonly IterationSchema Schema;

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new IterationLoaderBase with the associated schema config.
        /// </summary>
        /// <param name="schema">Specifies the schema configuration data.</param>
        internal IterationLoaderBase(IterationSchema schema)
        {
            PreCondition.AssertNotNull(schema, "schema");

            Schema = schema;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Loads the iteration data using the strategies of the underlying
        /// derivations.
        /// </summary>
        /// <param name="importMonitor">Specifies the data load monitor which is used to visualize the data import.</param>
        /// <param name="workItemDataLoader">Specifies the work item loader which is used to initialize the CacheWorkItem instances.</param>
        /// <param name="context">Specifies the context with all data which is required to import the data.</param>
        /// <param name="callback">Specifies the callback mechanism method which is used to call the next load part.</param>
        internal abstract void StartLoadIterationData(IDataImportMonitor importMonitor, WorkItemDataLoader workItemDataLoader, InitialDataCacheLoader.DataLoadContext context, Action<Exception> callback);

        protected string GetIterationPathName(string iterationPath)
        {
            return iterationPath.Substring(iterationPath.IndexOf(IterationPathSeparator) + 1);
        }

        protected void LoadIterationsRecursive(IList<Node> orderedIterations, IDictionary<string, Node> indexIterations, NodeCollection nodes)
        {
            foreach (Node iteration in nodes)
            {
                if (!indexIterations.ContainsKey(iteration.Path))
                {
                    indexIterations[iteration.Path] = iteration;
                    orderedIterations.Add(iteration);

                    LoadIterationsRecursive(orderedIterations, indexIterations, iteration.ChildNodes);
                }
            }
        }

        #endregion

        #region Events

        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}